clear all
set more off
capture log close
set matsize 11000 

*---- Set Directory ----*
global rootDir = "/Users/AliceZhang/Dropbox/Research_Columbia/Renewables Voting (Urpelainen Zhang)/JOP/UZ_JOP2021_Replication"
global dataDir = "Data"
global resultDir = "Results"
global logDir = "Analysis/logSTATA"
global graphDir = "Results/Figures"

*---- Log File Path ----*
cd "$rootDir/$logDir"
log using 006_analysis_turnout, replace


*******************************************************************************
/*				          URPELAINEN & ZHANG 2021	        				 */
*******************************************************************************

/* 

File Name:    	006_analysis_turnout.do

By:				Alice Tianbo Zhang (alice.tianbo.zhang@gmail.com)

Last Edited:  	10/13/2021

Purpose:		

Data Used:      election_turnout_district_panel.dta
				

Program Used:   - reghdfe -

*/


*******************************************************************************
/*                   	   	TURNOUT ANALYSIS BY PARTY ID		   		     */
*******************************************************************************
cd "$rootDir/$dataDir/Final"
use election_turnout_district_panel.dta, clear

** Create instrument and fixed effects
gen t = year - 2004
gen inter = t * mean_wp

egen stateyear_fixed = group(state year)
egen state_fixed = group(state)
egen district_fixed = group(state district)

gen cum_lncapacity_turbine = log(cum_capacity_turbine + 1)
gen cum_lncount_turbine = log(cum_count_turbine +1 )

local outcome dem_turnout rep_turnout ind_turnout // outcome variable 
local endogenous cum_lncapacity_turbine cum_lncount_turbine // endogenous variable
local instrument inter // instrument

foreach y in `outcome' {
	// Create outcome variable label for storing estimates
	local y_name = substr("`y'", 1, 3)
	
	foreach x in `endogenous' {
			// Create endogenous variable label for storing estimates
			tokenize "`x'", parse("_")
			local x_name "`3'"
			di "`x_name'"
			
			// Run IV regression
			reghdfe `y' (`x' = `instrument'), absorb(state_fixed year) ffirst stages(first ols reduced) vce(cluster district_fixed) old
			
			// Store IV, first stage, OLS, reduced form estimates
			estimates store `y_name'_`x_name'_iv
			estimates restore reghdfe_first1
			estimates store `y_name'_`x_name'_first
			estimates restore reghdfe_ols
			estimates store `y_name'_`x_name'_ols
			estimates restore reghdfe_reduced
			estimates store `y_name'_`x_name'_reduced
		}
}

*----------------- Export LaTeX tables - Turnout by party ID ----------------*
cd "$rootDir/$resultDir/Tables"

* First stage
esttab dem_lncapacity_first dem_lncount_first rep_lncapacity_first rep_lncount_first ///
		ind_lncapacity_first ind_lncount_first using TableA19.tex, booktabs replace ///
		refcat(inter "\emph{Panel A: First stage}", nolabel) ///
		b(%9.3f) se noconstant noobs nonotes star(* 0.10 ** 0.05 *** 0.01) ///
		varlabels(inter "Mean wind potential * time") varwidth(20) modelwidth(10) ///
		mtitles("Model" "Model" "Model" "Model" "Model" "Model") ///
		mgroups("Democratic Turnout" "Republican Turnout" "Independent Turnout", pattern(1 0 1 0 1 0) prefix(\multicolumn{@span}{c}{) suffix(}) span erepeat(\cmidrule(lr){@span})) ///
		width(\hsize)
		
	
* IV
esttab dem_lncapacity_iv dem_lncount_iv rep_lncapacity_iv rep_lncount_iv ///
		ind_lncapacity_iv ind_lncount_iv using TableA19.tex, booktabs append ///
		refcat(cum_lncapacity_turbine "\emph{Panel B: IV}", nolabel) ///
		nomtitles se noconstant nonotes legend nonumbers collabels(none) star(* 0.10 ** 0.05 *** 0.01) ///
		b(%9.3f) stats(N N_clust r2, labels("Observations" "Districts" "\(R^{2}\)") fmt(0 0 2 2)) ///
		varlabels(cum_lncapacity_turbine "log(Cumulative capacity+1)" cum_lncount_turbine "log(Cumulative count+1)") varwidth(27) modelwidth(13) ///
		width(\hsize)
		



** Close log file
log close
cd "$rootDir/$logDir"
translate 006_analysis_turnout.smcl 006_analysis_turnout.log, replace

